# -*- coding: utf-8 -*-
import pymongo
client = pymongo.MongoClient('47.75.39.50')
client.admin.authenticate("admin321", "dsf::6666,,<<", mechanism='SCRAM-SHA-1')
db = client.city4
data = set()
import re
import MySQLdb


for y in db.stores.find():
    # for yy in y['area']:
        # print yy
        # import ipdb;ipdb.set_trace()
    if y['area']:
        data.add((y['area'][3].encode("utf8"),y['area'][4].encode("utf8")))

db = MySQLdb.connect("127.0.0.1","root","123456789", "shantianjun",charset="utf8")
cursor = db.cursor()
sql = "select name from mode union select name from cate"
cursor.execute(sql)
cateLsit = cursor.fetchall()
names = set()
cateLsit = "|".join([x[0] for x in cateLsit])
ar = []
for y in data:
    name = y[1].decode("utf8")
    name = re.sub(cateLsit,"",name)
    topname = re.sub(cateLsit,"",y[0].decode("utf8"))
    if name in names:
        continue
    sql = "select id from area where name = '{}'".format(topname.encode("utf8"))
    cursor.execute(sql);
    # import ipdb;ipdb.set_trace()
    id = cursor.fetchone()[0]
    sql = "insert into area (name,level,tid) values (%s,%s,%s)"
    ar.append((name,3,id))
    names.add(name)

cursor.executemany(sql,ar)
cursor.close()
db.commit()
